﻿Imports MICROFORMA.BEL
Imports System.Data.Linq
Imports System.Data
Imports System.Transactions

Public Class ArbolDA
    Inherits ConexionDA

    Function Agregar(ByVal _Arbol As MICROFORMA.BEL.Arbol, ByVal _dataIndex As DataIndex) As Integer
        Dim index As Integer

        Using ts As New TransactionScope

            Dim dc As EntidadesDataContext = ObtenerDC()

            Try
                dc.Arbol.InsertOnSubmit(_Arbol)

                dc.SubmitChanges()

                _dataIndex.IdDocumento = _Arbol.IdDocumento

                dc.DataIndex.InsertOnSubmit(_dataIndex)

                dc.SubmitChanges()

                index = _Arbol.IdDocumento
            Catch ex As Exception
                index = 0
                ts.Dispose()
            End Try

            ts.Complete()
        End Using

        Return index

    End Function

    Function ObtenerDatos() As List(Of Arbol)
        Dim query As IEnumerable(Of Arbol)
        Dim dc As EntidadesDataContext = ObtenerDC()
        Try
            query = From Arbol In dc.Arbol Where Arbol.Estado = "0" Select Arbol
        Catch ex As Exception
            Throw ex
        End Try
        Return query.ToList
    End Function

    Function ObtenerHijos(ByVal _idDocumento As Int16) As List(Of Arbol)
        Dim query As IEnumerable(Of Arbol)
        Dim dc As EntidadesDataContext = ObtenerDC()
        Try
            query = From Arbol In dc.Arbol Select Arbol Where Arbol.IdPadre = _idDocumento And Arbol.Estado = "0"

        Catch ex As Exception
            Throw ex
        End Try
        Return query.ToList
    End Function


    Function Actualizar(ByVal Arbol As Arbol) As Integer
        Dim dc As EntidadesDataContext = ObtenerDC()
        Try
            dc.Arbol.Attach(Arbol)
            dc.Refresh(Data.Linq.RefreshMode.KeepCurrentValues, Arbol)
            dc.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
        Return 1
    End Function
    Function Eliminar(ByVal Arbol As Arbol) As Integer
        Dim dc As EntidadesDataContext = ObtenerDC()
        Try
            dc.Arbol.Attach(Arbol)

            dc.Arbol.DeleteOnSubmit(Arbol)
            dc.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try

        Return 1
    End Function
    Function DarBajaNodo(ByVal IdNodo As Int16) As Integer
        Dim dc As EntidadesDataContext = ObtenerDC()
        Try
            'obtenemos el nodo segun su ID
            Dim nodito = (From Arbol In dc.Arbol Where Arbol.IdDocumento = IdNodo Select Arbol).Single
            'cambiamos el estado del nodo
            nodito.Estado = "1"
            'grabamos los cambios en la BD
            dc.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try

        Return 1
    End Function

End Class
